<template>
  <div class="departments-container">
    <div class="app-container">
      <el-card class="box-card">
        <!-- <div
          slot="header"
          class="clearfix"
        > -->
        <TreeTool
          :tree-node="company"
          :is-root="true"
          @addDepts="addDepartmentsInfo"
        />
        <!-- </div> -->
        <el-tree
          :data="departs"
          :props="defaultProps"
          :default-expand-all="true"
        >
          <TreeTool
            slot-scope="{ data }"
            :tree-node="data"
            @delDepts="getDepartmentsInfo"
            @addDepts="addDepartmentsInfo"
            @editDepts="editDepartmentsInfo"
          />
        </el-tree>
      </el-card>
    </div>
    <AddDepart
      ref="addDept"
      :show-dialog.sync="showDialog"
      :tree-node="node"
      @updateDeparts="getDepartmentsInfo"
    />
  </div>
</template>

<script>
import TreeTool from './components/tree-tools.vue'
import { getDepartments } from '@/api/departments'
import { tranListToTreeDate } from '@/utils/index'
import AddDepart from './components/add-depart.vue'
export default {
  components: { TreeTool, AddDepart },
  data () {
    return {
      node: {},
      showDialog: false,
      defaultProps: {
        label: 'name'
      },
      company: {
        name: '创战纪科技股份有限公司',
        manager: '负责人',
        id: ''
      },
      departs: []
    }
  },
  created () {
    this.getDepartmentsInfo()
  },
  methods: {
    async getDepartmentsInfo () {
      const result = await getDepartments()
      // this.company = { name: result.companyName, manager: '负责人', id: '' }
      console.log(result)
      const tree = tranListToTreeDate(result.depts
        , '')
      this.departs = tree
      console.log(tree)
    },
    addDepartmentsInfo (data) {
      this.showDialog = true
      this.node = data
    },
    handleNodeClick (data) {
      console.log(data)
    },
    editDepartmentsInfo (data) {
      console.log(111)
      this.showDialog = true
      this.node = data
      this.$refs.addDept.getDepartDetail(data.id) // 直接调用子组件中的方法 传入一个id
    }
  }
}
</script>

<style scoped>
.box-card {
  padding: 30px 100px;
  font-size: 14px;
}
</style>
